package com.lyw.home

import android.content.Context
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.lu.library.base.BaseActivity
import com.lu.library.ext.navigation
import com.lu.library.ext.textChanges
import com.lyw.home.bean.SearchResult
import com.lyw.home.databinding.ActivitySearchBinding
import com.lyw.home.databinding.ItemSearchResultBinding
import com.lyw.home.vm.SearchVm
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import me.shouheng.utils.ktx.onDebouncedClick

@AndroidEntryPoint
class SearchActivity : BaseActivity<ActivitySearchBinding>() {

    private val searchVm by viewModels<SearchVm>()

    private val adapter =
        object : BaseQuickAdapter<SearchResult, DataBindingHolder<ItemSearchResultBinding>>() {
            override fun onBindViewHolder(
                holder: DataBindingHolder<ItemSearchResultBinding>,
                position: Int,
                item: SearchResult?
            ) {
                item?.apply {
                    holder.binding.tvType.text = type
                    holder.binding.tvContent.text = content
                    if (position >= 1) {
                        val pre = getItem(position - 1)
                        if (type == pre!!.type) {
                            holder.binding.tvType.isInvisible = true
                        } else {
                            holder.binding.tvType.isVisible = true
                        }
                    } else {
                        holder.binding.tvType.isVisible = true
                    }
                }
                holder.itemView.onDebouncedClick {
                    gotoDetail(item!!)
                }

            }

            override fun onCreateViewHolder(
                context: Context,
                parent: ViewGroup,
                viewType: Int
            ): DataBindingHolder<ItemSearchResultBinding> {
                return DataBindingHolder(R.layout.item_search_result, parent)
            }


        }
    fun gotoDetail(item: SearchResult){

        when(item.type){
            "作者"->{
//                ActivityUtils.startActivity(AuthorDetailActivity::class.java)
                HomeRouterPaths.HOME_AUTHOR_DETAIL.navigation("id",item.id)
            }
        }
    }
    override fun initData() {

        lifecycleScope.launch {

            searchVm.searchResult.collect {
                adapter.items = it
                adapter.notifyDataSetChanged()
            }
        }


    }

    override fun initViews() {
        super.initViews()


        binding.rvResult.layoutManager = LinearLayoutManager(this)
        binding.rvResult.adapter = adapter
        adjustNavBar(binding.rvResult)
    }

    override fun initListener() {
        super.initListener()
        binding.ivBack.onDebouncedClick {
            finish()
        }
        searchVm.searchKeyWord(binding.etKeyword.textChanges())
        binding.etKeyword.requestFocus()
    }

    override fun layoutResId(): Int = R.layout.activity_search
}